


import os
from PIL import Image
import platform

def base_dir():
    # 获取当前文件的目录
    base_dir = os.path.dirname(os.path.abspath(__file__))
    # 构造相对路径
    if platform.system() == "Windows":
        image_paths = "e:/report/images"
    elif platform.system() == "Darwin":  # macOS 系统返回的是 "Darwin"
        image_paths = "/Users/chenjian/code/bishe/selenium_test/pages/images"
    else:
        raise Exception("不支持的操作系统")
    return image_paths
img_dir = base_dir()

img_list = os.listdir(img_dir)  # 列出目录所有图片
img_list.sort(key=lambda x: int(x[:-4]))  # 排序

first_img = Image.open(os.path.join(img_dir, img_list[0]))  # 第一张图片对象
else_imgs = [Image.open(os.path.join(img_dir, img)) for img in img_list[1:]]  # 剩余图片对象

first_img.save(os.path.join(img_dir,"reco.gif"), append_images=else_imgs,
               duration=300,  # 每张图片的过过渡时间
               save_all=True) # 拼接保存，如果想要循环播放可以加上loop=0
